historic_NYPD_df = read_csv("./NYPD_Shooting_Incident_Data__Historic_.csv") %>%
  separate('OCCUR_DATE', into=c("month","day","year"),sep = "/") %>%
  filter(year>=2018)
## Rows: 23568 Columns: 19
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (10): OCCUR_DATE, BORO, LOCATION_DESC, PERP_AGE_GROUP, PERP_SEX, PERP_R...
## dbl   (5): INCIDENT_KEY, PRECINCT, JURISDICTION_CODE, Latitude, Longitude
## lgl   (1): STATISTICAL_MURDER_FLAG
## time  (1): OCCUR_TIME
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
year_to_date_df = read_csv("./NYPD_Shooting_Incident_Data__Year_To_Date_.csv")%>%
  separate('OCCUR_DATE', into=c("month","day","year"),sep = "/")
## Rows: 1531 Columns: 19
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (10): OCCUR_DATE, BORO, LOCATION_DESC, PERP_AGE_GROUP, PERP_SEX, PERP_R...
## dbl   (7): INCIDENT_KEY, PRECINCT, JURISDICTION_CODE, X_COORD_CD, Y_COORD_CD...
## lgl   (1): STATISTICAL_MURDER_FLAG
## time  (1): OCCUR_TIME
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
df = bind_rows(historic_NYPD_df,year_to_date_df)
df_period_before = df %>%
  arrange(year,month,day)%>%
  filter(year <= 2019) %>% 
  separate(OCCUR_TIME,into = c("hour","minute","second"),sep = ":")%>%
  mutate(hour=as.numeric(hour))%>%
  group_by(hour) %>%
  summarise(count=n(),
            time="Pre Covid")

df_period_after = df %>%
  arrange(year,month,day)%>%
  filter(year>= 2020) %>% 
  separate(OCCUR_TIME,into = c("hour","minute","second"),sep = ":")%>%
  mutate(hour=as.numeric(hour)) %>% 
  group_by(hour) %>%
  summarise(count=n(),
            time="During Covid")

df_period =
  bind_rows(df_period_before,df_period_after) %>% 
  plot_ly(
    x = ~ hour, y = ~ time, z = ~ count, type = "heatmap", colors = "YlGn"
  ) %>%
  colorbar(title = "Crimes Number", x = 1.1, y = 0.8) 

layout(df_period, title = "Crime frequency: Pre Covid and During Covid v.s. Hour", xaxis = list(title = "Hour"), yaxis = list(title = "Degree"), width = 850, height = 400)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
data_pre = 
  df %>% 
  janitor::clean_names() %>% 
  arrange(year,month,day)%>%
  filter(year <= 2019) %>%
  rename(long = longitude, lat = latitude) %>% 
  group_by(boro) %>% 
  mutate(crime_number = n(),
         crime_number = as.numeric(crime_number))
## Warning in FUN(X[[i]], ...): strings not representable in native encoding will
## be translated to UTF-8
## Warning in FUN(X[[i]], ...): unable to translate '<U+00C4>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00D6>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00E4>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00F6>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00DF>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00C6>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00E6>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00D8>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00F8>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00C5>' to native encoding
## Warning in FUN(X[[i]], ...): unable to translate '<U+00E5>' to native encoding
pal = colorNumeric(
  palette = "Reds",
  domain = data_pre$crime_number,
  na.color = "white")

data_pre %>% 
  mutate(
    label = str_c("<b>crime number: ", crime_number, "</b><br>location: ", lon_lat , sep = "") ) %>%
  leaflet() %>% 
  addTiles() %>%
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addCircles(lng = ~long, lat = ~lat, weight = 5, stroke = FALSE, radius = ~sqrt(crime_number)*6, popup = ~ label, color = ~pal(crime_number))%>% 
  addLegend("topright", pal = pal, values = ~crime_number,
    title = "2019-2020 Total Gun Crime",
    opacity = 1
  ) %>% 
   setView(-73.8399986, 40.746739, zoom = 10.5)
data_during = 
  df %>% 
  janitor::clean_names() %>% 
  arrange(year,month,day)%>%
  filter(year >= 2020) %>%
  rename(long = longitude, lat = latitude) %>% 
  group_by(boro) %>% 
  mutate(crime_number = n(),
         crime_number = as.numeric(crime_number))

pal = colorNumeric(
  palette = "Reds",
  domain = data_pre$crime_number)

data_during %>% 
  mutate(
    label = str_c("<b>crime number: ", crime_number, "</b><br>location: ", lon_lat , sep = "") ) %>%
  leaflet() %>% 
  addTiles() %>%
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addCircles(lng = ~long, lat = ~lat, weight = 5, stroke = FALSE, radius = ~sqrt(crime_number)*6, popup = ~ label, color = ~pal(crime_number))%>% 
  addLegend("topright", pal = pal, values = ~crime_number,
    title = "2020-2021 Total Gun Crime",
    opacity = 1
  ) %>% 
 setView(-73.8399986, 40.746739, zoom = 10.5)
## Warning in pal(crime_number): Some values were outside the color scale and will
## be treated as NA

## Warning in pal(crime_number): Some values were outside the color scale and will
## be treated as NA
## Warning in pal(c(r[1], cuts, r[2])): Some values were outside the color scale
## and will be treated as NA
unemploy = 
  read_csv("Unemployment.csv") %>% 
  separate(label, into = c("year", "month")) %>% 
  filter(year != "2017")
## Rows: 2900 Columns: 5
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (3): series_id, period, label
## dbl (2): year, value
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
unemploy = dplyr::rename(unemploy, state = series_id)

gun_violence = 
  read_csv("data/year_month_state_massshooting.csv") %>% 
  mutate(year = year + 2000) %>% 
  mutate(year = as.character(year))
## New names:
## * `` -> ...1
## Rows: 911 Columns: 5
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): month, state
## dbl (3): ...1, year, number_mass_shooting
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
new_dataframe = 
  right_join(unemploy, gun_violence, by = c("state" = "state", "year" = "year", "month" = "month")) %>% 
  mutate(period = str_remove(period, "[M]"),
         period = as.numeric(period))